const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const glob = require("glob");

const devMode = process.env.NODE_ENV !== "production";
const htmls = glob.sync("src/pages/**/*.html");
const entrys = {}; 
const htmlCfgs = [];
htmls.forEach((filePath) => { //遍历扫描到的页面模板路径
    let paths = filePath.split('/'); //分割路径,
    let file = paths.pop(); //把入口模板页面文件名pop出来, 比如: help.html
    let name = file.split('.')[0]; //把入口页面名分割出来, 取第一个就是 help
    entrys[name] = path.resolve(__dirname, "../src/pages/"+ name + '/' + name + ".js"); //动态配置入口文件路径
    htmlCfgs.push( //动态配置入口文件插件
        new HtmlWebpackPlugin({
            filename:file,
            inject: 'head',
            template: filePath,
            chunks: [name],
            inlineSource: '.(js|css)$',
            minify:{
                removeComments: true,//删除注释
                collapseWhitespace:true//删除空格
            }
        }),
    )
});

htmlCfgs.push(new CleanWebpackPlugin())
htmlCfgs.push(new UglifyJsPlugin())

module.exports = {
    entry: entrys,
    output: {
        path: path.resolve(__dirname, "../dist"),
        filename: 'js/[name]-[hash:5].js',
        publicPath: "/",

    },
    resolve: {
        extensions: ['.js', '.scss', '.css'],
        alias: {
            '@': path.resolve(__dirname, "../src"),
            'assets': path.resolve(__dirname, "../src/assets")
        }
    },
    module: {
        rules: [
            {
                test: /.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ['es2015','stage-1'],
                        plugins: ["transform-runtime", "babel-plugin-transform-regenerator", "babel-plugin-transform-es2015-modules-commonjs"]
                    }
                }
            },
            // css处理
            {
                test: /\.css$/,
                use: [
                   'style-loader',
                   'css-loader'
                ]

            },
            {
                test: /\.scss$/,
                use: [
                   'style-loader',
                   'css-loader',
                   'sass-loader'
                ]
            },
             // 图片处理
            {
            test: /\.(png|jpg|gif)$/,
            exclude: /node_modules/,
            use: [
              {
                loader: "url-loader", //根据文件地址加载文件
                options: {
                  limit: 2048,
                  name: "[name].[ext]",
                  outputPath: "../dist/static",
                  publicPath: "../static",
                },
              },
            ],
            },
            {
                test:/\.html$/,
                use:[
                    {
                        loader:"html-loader",
                        options:{
                            attrs:["img:src"]
                        }
                    }
                ]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
                loader: 'file-loader',
                options: {
                    limit: 10000,
                    name: 'assets/[name]-[hash:5].[ext]'
                }
            }
        ]
    },
    plugins: htmlCfgs
};
